package com.xiangyu.demo.controller;

import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;

/**
 * java类简单作用描述
 *
 * @ProjectName: springboot-demo
 * @Package: com.xiangyu.demo.controller
 * @Description: java类作用描述
 * @Author: 向羽
 * @CreateDate: 2018/1/29 15:53
 * @UpdateUser: 向羽
 * @UpdateDate: 2018/1/29 15:53
 * @UpdateRemark: The modified content
 * @Version: 2.0
 */
@RestController
@RequestMapping("/file")
public class FileController {

    @RequestMapping(value="/image", method = RequestMethod.POST)
    public String uploadImg(@RequestParam("file") MultipartFile file) {
        String fileName = file.getOriginalFilename();
        try {
            String url = ResourceUtils.getURL("classpath:").getPath();
            File upload = new File( url+File.separator+"static.upload.admin");
            if (!upload.exists()){
                upload.mkdirs();
            }
            file.transferTo(new File(upload,fileName));
        } catch (Exception e) {
            e.printStackTrace();
        }
        //返回json
        return "uploadimg success";
    }

}
